﻿using Newtonsoft.Json;
using System;

namespace CMS.Infos
{
    public class CourseAdjust : ICloneable, IAssignable
    {
        #region 私有字段
        private string _FromDate;

        private string _ToDate;

        private bool _IsHoliday;

        private DateTime? _FromDateRaw;

        private DateTime? _ToDateRaw;

        #endregion


        #region 公共属性
        [JsonProperty(PropertyName = "from_date")]
        public string FromDate
        {
            get => _FromDate;
            set
            {
                _FromDateRaw = null;
                _FromDate = value;
            }
        }

        [JsonProperty(PropertyName = "to_date")]
        public string ToDate
        {
            get => _ToDate;
            set
            {
                _ToDateRaw = null;
                _ToDate = value;
            }
        }

        [JsonProperty(PropertyName = "status")]
        public bool Status
        {
            get;
            set;
        }

        [JsonIgnore]
        public bool IsHoliday
        {
            get => _IsHoliday;
            set
            {
                _IsHoliday = value;
                if (value)
                {
                    ToDate = null;
                }
            }
        }

        [JsonIgnore]
        public DateTime FromDateRaw
        {
            get
            {
                if (_FromDateRaw == null)
                {
                    if (_FromDate == null)
                    {
                        _FromDateRaw = DateTime.Now.Date;
                        _FromDate = _FromDateRaw.Value.ToString("yyyy-MM-dd");
                        return _FromDateRaw.Value;
                    }
                    _FromDateRaw = DateTime.Parse(_FromDate);
                }
                return _FromDateRaw.Value;
            }
            set
            {
                _FromDateRaw = value;
                _FromDate = value.ToString("yyyy-MM-dd");
            }
        }

        [JsonIgnore]
        public DateTime ToDateRaw
        {
            get
            {
                if (_ToDateRaw == null)
                {
                    if (_ToDate == null)
                    {
                        _ToDateRaw = DateTime.Now.Date;
                        _ToDate = _ToDateRaw.Value.ToString("yyyy-MM-dd");
                        return _ToDateRaw.Value;
                    }
                    _ToDateRaw = DateTime.Parse(_ToDate);
                }
                return _ToDateRaw.Value;
            }
            set
            {
                if (!_IsHoliday)
                {
                    _ToDateRaw = value;
                    _ToDate = value.ToString("yyyy-MM-dd");
                }
            }
        }

        public object Assign(object src)
        {
            if (src is CourseAdjust src2)
            {
                FromDate = src2.FromDate;
                ToDate = src2.ToDate;
                Status = src2.Status;
                return this;
            }
            else
            {
                throw new ArgumentException();
            }
        }

        public object Clone()
        {
            return new CourseAdjust().Assign(this);
        }

        #endregion

        #region 通用性重写
        public override string ToString()
        {
            return $"{FromDate}->{ToDate}";
        }

        public override int GetHashCode()
        {
            return (_FromDate?.GetHashCode() ?? 0) ^
                (_ToDate?.GetHashCode() ?? 0) ^
                Status.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj is CourseAdjust obj2)
            {
                return _FromDate == obj2._FromDate && _ToDate == obj2._ToDate && Status == obj2.Status;
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}
